#-*- coding: utf8 -*-
from tutor.scripts import *
from tutor.plugins.maple import *

# variáveis úteis
half = M('1/2')
t = M.t
_i, _j, _k = M('_i, _j, _k')
rr = M('`..`')
diff = M.diff
coeff, sqrt = M.coeff, M.sqrt
r = M.r
t = M.t
def polar(F):
    return M.subs(x == r * cos(t), y == r * sin(t), F)

# parâmetros iniciais
C = [ oneof(1, 1, -1) * oneof(1, 1, 2, 3, 4),
      oneof(1, 1, -1) * oneof(1, 1, 2, 3, 4),
      oneof(1, 1, -1) * oneof(1, 1, 2, 3, 4) ]
vars = [x, y, z]
shuffle(vars)

# monta vetor campo
Fx, Fy, Fz = [ c * v for c, v in zip(C, vars) ]
Fz += 1
F = fieldF = Fx * _i + Fy * _j + Fz * _k
print('campo:', fieldF)

# superficie inferior (plana)
aux = M.int(-polar(subs(z == 1, Fz)), t=rr(0, 2 * M.Pi))
PhiSub = M.simplify(M.int(aux * r, r=rr(0, 1)))

# superficie superior (parabolóide)
f = 1 - x ** 2 + y ** 2
fx = diff(f, x)
fy = diff(f, y)
JS = M.simplify(M.subs(z == f, -fx * Fx - fy * Fy + Fz))
aux = M.int(polar(JS), t=rr(0, 2 * M.Pi))
PhiSup = M.simplify(M.int(aux * r, r=rr(0, 1)))

# resposta correta 
Phi = M.simplify(PhiSub + PhiSup)
print('fluxo: ', Phi)

# omitiu a superfície inferior
PhiParab = PhiSup
print('superior: ', PhiParab)

# misturou a normal (escolheu todas as normais para cima)
PhiMix = M.simplify(PhiSup - PhiSub)
print('misturado: ', PhiMix)

# omitiu o jacobiano
aux = M.int(-polar(subs(z == 1, Fz)), t=rr(0, 2 * M.Pi))
PhiJ = M.simplify(M.int(aux, r=rr(0, 1)))
aux = M.int(polar(JS), t=rr(0, 2 * M.Pi))
PhiJ = M.simplify(M.int(aux, r=rr(0, 1)) + PhiJ)
print('jacobiano: ', PhiJ)

# região quadrada
aux = M.int(-subs(z == 1, Fz), x=rr(-1, 1))
PhiQ = M.simplify(M.int(aux, y=rr(-1, 1)))
aux = M.int(JS, x=rr(-1, 1))
PhiQ = M.simplify(M.int(aux, y=rr(-1, 1)) + PhiQ)
print('quadrado: ', PhiQ)
